package com.xg.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xg.sys.entity.SysRoleMenu;
import com.xg.sys.mapper.SysRoleMenuMapper;
import com.xg.sys.service.SysRoleMenuService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色与菜单对应关系 服务实现类
 * </p>
 *
 * @author ChenLiang
 * @since 2019-04-30
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    public List<Long> selectRoleMenuList(Long id) {
        return baseMapper.selectRoleMenuList(id);
    }

    @Override
    public boolean saveRoleOrUpdateRole(Long roleId, List<Long> menuIdList) {

        //先删除角色与菜单的关系
        deleteBatch(new Long[]{roleId});

        boolean result = false;

        //保存角色与菜单关系
        if (CollectionUtils.isNotEmpty(menuIdList)) {
            List<SysRoleMenu> roleMenuList = menuIdList.stream()
                    .map(item -> new SysRoleMenu().setRoleId(roleId).setMenuId(item))
                    .collect(Collectors.toList());
            result = this.saveBatch(roleMenuList);
        }
        return result;
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }

    @Override
    public List<SysRoleMenu> getRoleMenuByRidList(List<Long> ridList) {
        return this.list(
                new LambdaQueryWrapper<SysRoleMenu>()
                        .in(SysRoleMenu::getRoleId, ridList)
        );
    }


}
